home *** CD-ROM | disk | FTP | other *** search
- unit Vatform;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, XPMan;
-
- type
- // Note the definition of the form TForm1 is entered by Delphi and this
- // code should not be altered
- TForm1 = class(TForm)
- CalcBtn: TButton;
- GroupBox1: TGroupBox;
- SubTotRB: TRadioButton;
- GrandTotRB: TRadioButton;
- SubTotal: TEdit;
- Vat: TEdit;
- GrandTotal: TEdit;
- Label1: TLabel;
- XPManifest1: TXPManifest;
- procedure CalcBtnClick(Sender: TObject);
- procedure SubTotRBClick(Sender: TObject);
- procedure GrandTotRBClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure InputError( TE: TEdit; errcode : integer );
- // given an integer, errcode, and an edit box, TE, this procedure selects
- // (that is, it highlights) the character at the index given by the TE.SelStart
- // property. It slao displays the character in an error message
- var
- Msg : string;
- begin
- if TE.Text = '' Then
- Msg := 'You must enter a value'
- else
- Msg := 'Invalid character: ' + Copy(TE.Text, errcode, 1);
- MessageDlg(Msg, mtError,
- [mbOk], 0);
- TE.SetFocus;
- TE.SelStart := errcode-1;
- TE.SelLength := 1;
- end;
-
- procedure PlusVat( var subtot, vattot, grandtot : real );
- // given real value subtot, calculates VAT and assigns this to
- // vattot. Calculates VAT inclusive total and assigns this to
- // granddot. Note that all 3 input arguments to this procedure
- // are preceded by the VAR keyword. This means that they are passed
- // 'by reference' so that any changes made inside the procuedure affect
- // the original variables that were passed to the procedure.
- begin
- vattot := (subtot * 0.175);
- grandtot := vattot + subtot;
- end;
-
- procedure MinusVat( var subtot, vattot, grandtot : real );
- // given real value grandtot, calculates VAT and assigns this to
- // vattot. Calculates VAT exclusive total and assigns this to
- // subdot.
- begin
- vattot := (grandtot * 0.14894);
- subtot := grandtot - vattot;
- end;
-
- procedure TForm1.CalcBtnClick(Sender: TObject);
- var
- stxt, vtxt,
- gtxt : string;
- st, vt, gt : real;
- errcode : integer;
- begin
- st := 0.0;
- vt := 0.0;
- gt := 0.0;
- stxt := '';
- vtxt := '';
- gtxt := '';
- // These if...else blocks take different actions according to which
- // radio button is checked on the form. If the SubTotRB button is
- // checked, the grandtotal is calculated from the subtotal which the
- // user has entered. Otherwise, the subtotal is calculated from
- // the grand total which has been entered
- if SubTotRB.Checked Then
- begin
- Val(SubTotal.Text, st, errcode);
- if errcode <> 0 then InputError(SubTotal,
- errcode);
- begin
- PlusVat(st,vt,gt);
- Str(vt:2:2, vtxt );
- Str(gt:2:2, gtxt );
- Vat.Text := vtxt;
- GrandTotal.Text := gtxt;
- end;
- end // note: no semi-colon prior to the 'else' keyword
- else
- begin
- Val(GrandTotal.Text, gt, errcode);
- if errcode <> 0 then InputError( GrandTotal, errcode );
- begin
- MinusVat(st,vt,gt);
- Str(vt:2:2, vtxt );
- Str(st:2:2, stxt );
- Vat.Text := vtxt;
- SubTotal.Text := stxt;
- end;
- end;
- end;
-
- procedure TForm1.SubTotRBClick(Sender: TObject);
- begin
- CalcBtn.Caption := '&Calculate the Grand Total';
- end;
-
- procedure TForm1.GrandTotRBClick(Sender: TObject);
- begin
- CalcBtn.Caption := '&Calculate the Sub Total';
- end;
-
- end.
-